// pages/login/login.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: null,
    pwd: null

  },

  getPhoneNumber: function(e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },

  getPwd: function(e) {
    this.setData({
      pwd: e.detail.value
    })
  },

  loginButton: function() {
    var that = this;
    wx.request({
      url: 'http://localhost:8081/login/' + that.data.phoneNumber + '/' + that.data.pwd,
      method: "POST",
      success(res) {
        if (res.data == "IdError") {
          wx.showToast({
            title: '账号错误',
            image: '../../img/login/error.png',
            duration: 2000
          })
        } else if (res.data == "PwdError") {
          wx.showToast({
            title: '秘密错误',
            image: '../../img/login/error.png',
            duration: 2000
          })
        } else {
          app.data.userInfo = [{
            'userName': res.data,
            'userId': that.data.phoneNumber
          }]
          var len = getCurrentPages().length
          console.log(getCurrentPages())
          if (len==1){
            wx.switchTab({
              url: '../personal/personal',
            })
          }else if (getCurrentPages()[len - 2].route == 'pages/shop/shop') {
            wx.navigateTo({
              url: '../pay/pay',
            })
          }else{
            wx.switchTab({
              url: '../personal/personal',
            })
          }

        }
      }
    })
  },

  goReg: function() {
    wx.navigateTo({
      url: '../registered/registered',
    })
  }
})